package cheme.ProcessModel;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class Logger {
	File filehndl;
	FileOutputStream fileOut;
	PrintStream printStr;
	String fileName = "C:\\ChemProV_StudyLogs\\ChemProV_NoFeedback_LogFile";
	
	public Logger()
	{
		try
		{
			final String tempName = fileName;
			fileName = fileName + ".txt";
			filehndl = new File(fileName);
			// if file does not exists Create one
			if(!filehndl.exists())
			{
				filehndl.createNewFile();
			}
			// if file already exists, create another file
			else
			{
				int count = 1;
				while(filehndl.exists())
				{
					fileName = tempName + Integer.toString(count)+ ".txt"; // looger1.txt,logger2.txt
					filehndl = new File(fileName);
					count++;
				}
				filehndl.createNewFile();				
			}
			// Set up the output stream
			fileOut = new FileOutputStream(fileName);
			printStr = new PrintStream(fileOut);
		}
		catch (final IOException e)
		{
			e.printStackTrace();
		}		
	}
	
	public void Write(String event,String location, String message)
	{
		// Printint format - TimeStamp, Event, Location, Actual Message
		/*Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat("H:mm:ss");
	    String timeStamp = sdf.format(cal.getTime());*/
		Date currentDate = new Date();
		long currentTime = currentDate.getTime();
		// get intial time when application was launched and subtract ot from curent time to get the offset
		currentTime = currentTime - ProcessModelConstants.initialTime;
		// Convert the time into hh:mm:ss format for printing
		long currentTimeinsecs = currentTime/1000;
		
		long currenthr = currentTimeinsecs / (60*60);
		long tempmin = currentTimeinsecs % (60*60);
		long currentmin = tempmin /60;
		long currentsec = tempmin %60;
		
		
		 String timeStamp = Long.toString(currenthr) + ":" + Long.toString(currentmin) + ":" + Long.toString(currentsec);
		

	    printStr.print(timeStamp);
	    printStr.print(", ");
	    printStr.print(event);	    
		//String elementStr = "(" + element + "): " ; 		
		printStr.print(location);
		printStr.println(message);
	}
	
	public void printSpace()
	{
		printStr.println(" ");
		printStr.println("*******************************************************");
		printStr.println(" ");
	}
}
